package easy;

public class Solution_543 {

    private int max = 0;

    public int diameterOfBinaryTree(TreeNode root) {
        dps(root);
        return max;
    }

    public int dps(TreeNode node) {
        if (node == null) {
            return 0;
        }
        int left = dps(node.left);
        int right = dps(node.right);
        max = Math.max(max, left + right);
        int v = Math.max(left, right) + 1;
        System.out.println("node.val:"+node.val + "; level:" + v);
        return Math.max(left, right) + 1;
    }

    public static void main(String[] args) {
        Solution_543 model = new Solution_543();
        Solution_543.TreeNode root = new Solution_543.TreeNode(1);
        root.left = new Solution_543.TreeNode(2);
        root.left.left = new Solution_543.TreeNode(4);
        root.left.left.right = new Solution_543.TreeNode(8);
        root.left.right = new Solution_543.TreeNode(5);
        root.left.right.left = new Solution_543.TreeNode(6);
        root.left.right.right = new Solution_543.TreeNode(7);
        root.right = new Solution_543.TreeNode(3);
//        root.right.left = new Solution_543.TreeNode(5);
//        root.right.right = new Solution_543.TreeNode(6);
        System.out.println(model.diameterOfBinaryTree(root));
    }

    static class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }
}
